import { defineStore } from 'pinia'
import { EXPLOSION } from '@/utils/global'

export const useExplosionStore = defineStore('explosion', {
  state: () => ({
    explosions: [],
    id: 0
  }),
  actions: {
    bornExplosion({ x, y, staff = 'bullet' }) {
      const o = {
        id: this.id++,
        x,
        y, // (x, y)爆炸点坐标
        staff, // bullet or tank
        index: 0, // current index of process
        prog: 0,
      }

      this.explosions.push(o)
    },
    playExplosion(ft) {
      this.explosions.forEach(v => {
        if (v.prog >= EXPLOSION[v.staff].process[v.index].dur) {
          if (v.index >= EXPLOSION[v.staff].process.length - 1) {
            this.explosions = this.explosions.filter(t => t.id !== v.id)
          } else {
            this.explosions = this.explosions.map(t => t.id === v.id ? { ...t, prog: 0, index: t.index + 1 } : t)
          }
        } else {
          this.explosions = this.explosions.map(t => t.id === v.id ? { ...t, prog: t.prog + ft } : t)
        }
      })
    }
  }
})